package com.selfdriving.adapter;

import java.util.List;

import com.selfdriving.R;
import com.selfdriving.bean.Contact;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ContactAdapter extends BaseAdapter {

	private Context context;

	private List<Contact> contacts;

	private HolderView holderView;

	public ContactAdapter(Context context, List<Contact> contacts) {
		this.contacts = contacts;
		this.context = context;
	}

	@Override
	public int getCount() {
		return contacts.size();
	}

	@Override
	public Object getItem(int arg0) {
		return contacts.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View view, ViewGroup arg2) {
		Contact contact = contacts.get(arg0);
		if (view == null) {
			holderView = new HolderView();
			view = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.contact_item, null);
			view.setTag(holderView);
		} else {
			holderView = (HolderView) view.getTag();
		}
		holderView.name = (TextView) view.findViewById(R.id.tvName);
		holderView.phone = (TextView) view.findViewById(R.id.tvPhone);
		holderView.name.setText(contact.name);
		holderView.phone.setText(contact.phone);
		return view;
	}

	private class HolderView {
		private TextView name;

		private TextView phone;
	}

}
